package com.nahan.myphoto.ViewPicture.previewlibrary;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;

import com.kaopiz.kprogresshud.KProgressHUD;
import com.nahan.myphoto.R;


public class GPVideoPlayerActivity extends Activity {

    VideoView videoView;
    RelativeLayout video_view_back;
    protected KProgressHUD mProgressDialog;

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gpvideoplayer);
        videoView=findViewById(R.id.gpVideo);
        video_view_back=findViewById(R.id.video_view_back);

        videoView.setVideoPath(getIntent().getStringExtra("url"));

        showProgress();
        videoView.setOnInfoListener(new MediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(MediaPlayer mp, int what, int extra) {

                //这段代码的关键是MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START这个变量，
                // Android SDK中给出的注释是：这个状态表示展现了用于渲染的第一帧视频，也就是这个时候才真正将视频帧展示到了屏幕上。
                if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                    hideProgress();
                }
                return true;
            }
        });


        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                hideProgress();
                Toast.makeText(GPVideoPlayerActivity.this, R.string.Playback_failed,Toast.LENGTH_SHORT).show();
                finish();
                return false;
            }
        });
        videoView.start();
        video_view_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!videoView.isPlaying()){
            videoView.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        videoView.pause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        videoView.stopPlayback();
    }

    /***
     * 启动播放视频
     * @param   context context
     * @param  url url
     * ***/
    public static  void startActivity(Context context, String url){
        Intent intent=new Intent(context, GPVideoPlayerActivity.class);
        intent.putExtra("url",url);
        context.startActivity(intent);
    }


    /**
     * 显示进度对话框 (根据需要，子类可以选择性重写)
     **/
    public void showProgress() {
        if (mProgressDialog == null) {
            mProgressDialog = KProgressHUD.create(this)
                    .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE);
        }
        if (mProgressDialog.isShowing()) {
            return;
        }
        mProgressDialog.show();
    }

    /* 根据需要，子类可以选择性重写*/
    public void hideProgress() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }
}
